
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>3.bind的模拟实现 · Gary的小窝</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="Gary">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-prism/prism-solarizedlight.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="proto.html" />
    
    
    <link rel="prev" href="el.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    写在前面
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../leetcode/">
            
                <a href="../leetcode/">
            
                    
                    Leetcode之旅
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../leetcode/1.html">
            
                <a href="../leetcode/1.html">
            
                    
                    1.两数之和
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../leetcode/2.html">
            
                <a href="../leetcode/2.html">
            
                    
                    2.两数相加
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../leetcode/3.html">
            
                <a href="../leetcode/3.html">
            
                    
                    3.无重复字符的最长子串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../leetcode/4.html">
            
                <a href="../leetcode/4.html">
            
                    
                    4.寻找两个有序数组的中位数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../leetcode/6.html">
            
                <a href="../leetcode/6.html">
            
                    
                    6.Z字形变换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../leetcode/9.html">
            
                <a href="../leetcode/9.html">
            
                    
                    9.回文数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../leetcode/11.html">
            
                <a href="../leetcode/11.html">
            
                    
                    11.盛最多水的容器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="../leetcode/12.html">
            
                <a href="../leetcode/12.html">
            
                    
                    12.整数转罗马数字
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="../leetcode/13.html">
            
                <a href="../leetcode/13.html">
            
                    
                    13.罗马数字转整数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10" data-path="../leetcode/14.html">
            
                <a href="../leetcode/14.html">
            
                    
                    14.最长公共前缀
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.11" data-path="../leetcode/322.html">
            
                <a href="../leetcode/322.html">
            
                    
                    322.零钱兑换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.12" data-path="../leetcode/72.html">
            
                <a href="../leetcode/72.html">
            
                    
                    72.编辑距离
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../flutter/">
            
                <a href="../flutter/">
            
                    
                    Flutter从入门到放弃
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../flutter/dart1.html">
            
                <a href="../flutter/dart1.html">
            
                    
                    1.Dart语言(1)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../flutter/dart2.html">
            
                <a href="../flutter/dart2.html">
            
                    
                    2.Dart语言(2)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../flutter/widget1.html">
            
                <a href="../flutter/widget1.html">
            
                    
                    3.Flutter初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../flutter/containerText.html">
            
                <a href="../flutter/containerText.html">
            
                    
                    4.Container和Text组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../flutter/image.html">
            
                <a href="../flutter/image.html">
            
                    
                    5.Image组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../flutter/chapter2.html">
            
                <a href="../flutter/chapter2.html">
            
                    
                    6.Flutter 技术总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../vue/">
            
                <a href="../vue/">
            
                    
                    Vue组件精讲
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../vue/1.html">
            
                <a href="../vue/1.html">
            
                    
                    1.provide/inject
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../vue/2.html">
            
                <a href="../vue/2.html">
            
                    
                    2.手动实现broadcast和dispatch方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="form.html">
            
                <a href="form.html">
            
                    
                    3.自己动手实现带校验的Form表单组件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="./">
            
                <a href="./">
            
                    
                    JavaScript进阶
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="proxy.html">
            
                <a href="proxy.html">
            
                    
                    1.Vue.js 3.0中的响应式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="el.html">
            
                <a href="el.html">
            
                    
                    2.Element UI 源码初探
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5.3" data-path="bind.html">
            
                <a href="bind.html">
            
                    
                    3.bind的模拟实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="proto.html">
            
                <a href="proto.html">
            
                    
                    4.JS原型和原型链
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="call.html">
            
                <a href="call.html">
            
                    
                    5.模拟实现call和apply
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="ui.html">
            
                <a href="ui.html">
            
                    
                    6.使用Vue cli3搭建组件库并发布到 npm
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="microFE.html">
            
                <a href="microFE.html">
            
                    
                    7.记一次基于qiankun的微前端的改造
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.8" data-path="git.html">
            
                <a href="git.html">
            
                    
                    8.Git的使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../vueLibrary/">
            
                <a href="../vueLibrary/">
            
                    
                    基于Vue的PC端的组件库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../vueLibrary/1.html">
            
                <a href="../vueLibrary/1.html">
            
                    
                    1.环境搭建
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../life/">
            
                <a href="../life/">
            
                    
                    网事杂谈
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../life/1.html">
            
                <a href="../life/1.html">
            
                    
                    1.周杰伦专辑赏析--叶惠美
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../life/2.html">
            
                <a href="../life/2.html">
            
                    
                    2.周杰伦专辑赏析--七里香
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >3.bind的模拟实现</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#javascript&#x8FDB;&#x9636;&#x4E4B;bind&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;"><b></b>JavaScript&#x8FDB;&#x9636;&#x4E4B;bind&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;</a></li><ul><li><span class="title-icon "></span><a href="#bind"><b></b>bind</a></li><li><span class="title-icon "></span><a href="#&#x8FD4;&#x56DE;&#x51FD;&#x6570;&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;"><b></b>&#x8FD4;&#x56DE;&#x51FD;&#x6570;&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;</a></li><li><span class="title-icon "></span><a href="#&#x4F20;&#x53C2;&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;"><b></b>&#x4F20;&#x53C2;&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;</a></li><li><span class="title-icon "></span><a href="#&#x6784;&#x9020;&#x51FD;&#x6570;&#x6548;&#x679C;&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;"><b></b>&#x6784;&#x9020;&#x51FD;&#x6570;&#x6548;&#x679C;&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;</a></li><li><span class="title-icon "></span><a href="#&#x6784;&#x9020;&#x51FD;&#x6570;&#x6548;&#x679C;&#x7684;&#x4F18;&#x5316;&#x5B9E;&#x73B0;"><b></b>&#x6784;&#x9020;&#x51FD;&#x6570;&#x6548;&#x679C;&#x7684;&#x4F18;&#x5316;&#x5B9E;&#x73B0;</a></li><li><span class="title-icon "></span><a href="#&#x4E09;&#x4E2A;&#x5C0F;&#x95EE;&#x9898;"><b></b>&#x4E09;&#x4E2A;&#x5C0F;&#x95EE;&#x9898;</a></li><li><span class="title-icon "></span><a href="#&#x6700;&#x7EC8;&#x4EE3;&#x7801;"><b></b>&#x6700;&#x7EC8;&#x4EE3;&#x7801;</a></li></ul></ul></div><a href="#javascript&#x8FDB;&#x9636;&#x4E4B;bind&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h1 id="javascript&#x8FDB;&#x9636;&#x4E4B;bind&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;"><a name="javascript&#x8FDB;&#x9636;&#x4E4B;bind&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;" class="anchor-navigation-ex-anchor" href="#javascript&#x8FDB;&#x9636;&#x4E4B;bind&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;"><i class="fa fa-link" aria-hidden="true"></i></a>JavaScript&#x8FDB;&#x9636;&#x4E4B;bind&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;</h1>
<blockquote>
<p>&#x901A;&#x8FC7;bind&#x51FD;&#x6570;&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#xFF0C;&#x5E26;&#x5927;&#x5BB6;&#x771F;&#x6B63;&#x4E86;&#x89E3;bind&#x7684;&#x7279;&#x6027;</p>
</blockquote>
<h2 id="bind"><a name="bind" class="anchor-navigation-ex-anchor" href="#bind"><i class="fa fa-link" aria-hidden="true"></i></a>bind</h2>
<p>&#x4E00;&#x53E5;&#x8BDD;&#x4ECB;&#x7ECD; bind:</p>
<blockquote>
<p>bind() &#x65B9;&#x6CD5;&#x4F1A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x51FD;&#x6570;&#x3002;&#x5F53;&#x8FD9;&#x4E2A;&#x65B0;&#x51FD;&#x6570;&#x88AB;&#x8C03;&#x7528;&#x65F6;&#xFF0C;bind() &#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x5C06;&#x4F5C;&#x4E3A;&#x5B83;&#x8FD0;&#x884C;&#x65F6;&#x7684; this&#xFF0C;&#x4E4B;&#x540E;&#x7684;&#x4E00;&#x5E8F;&#x5217;&#x53C2;&#x6570;&#x5C06;&#x4F1A;&#x5728;&#x4F20;&#x9012;&#x7684;&#x5B9E;&#x53C2;&#x524D;&#x4F20;&#x5165;&#x4F5C;&#x4E3A;&#x5B83;&#x7684;&#x53C2;&#x6570;&#x3002;(&#x6765;&#x81EA;&#x4E8E; MDN )</p>
</blockquote>
<p>&#x7531;&#x6B64;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x9996;&#x5148;&#x5F97;&#x51FA; bind &#x51FD;&#x6570;&#x7684;&#x4E24;&#x4E2A;&#x7279;&#x70B9;&#xFF1A;</p>
<ol>
<li>&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x51FD;&#x6570;</li>
<li>&#x53EF;&#x4EE5;&#x4F20;&#x5165;&#x53C2;&#x6570;</li>
</ol>
<h2 id="&#x8FD4;&#x56DE;&#x51FD;&#x6570;&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;"><a name="&#x8FD4;&#x56DE;&#x51FD;&#x6570;&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;" class="anchor-navigation-ex-anchor" href="#&#x8FD4;&#x56DE;&#x51FD;&#x6570;&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x8FD4;&#x56DE;&#x51FD;&#x6570;&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;</h2>
<p>&#x4ECE;&#x7B2C;&#x4E00;&#x4E2A;&#x7279;&#x70B9;&#x5F00;&#x59CB;&#xFF0C;&#x6211;&#x4EEC;&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre class="language-"><code class="lang-js"><span class="token keyword">var</span> foo <span class="token operator">=</span> <span class="token punctuation">{</span>
    value<span class="token punctuation">:</span> <span class="token number">1</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">function</span> <span class="token function">bar</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// &#x8FD4;&#x56DE;&#x4E86;&#x4E00;&#x4E2A;&#x51FD;&#x6570;</span>
<span class="token keyword">var</span> bindFoo <span class="token operator">=</span> <span class="token function">bar</span><span class="token punctuation">.</span><span class="token function">bind</span><span class="token punctuation">(</span>foo<span class="token punctuation">)</span><span class="token punctuation">;</span> 

<span class="token function">bindFoo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 1</span>
</code></pre>
<p>&#x5173;&#x4E8E;&#x6307;&#x5B9A; this &#x7684;&#x6307;&#x5411;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; call &#x6216;&#x8005; apply &#x5B9E;&#x73B0;&#xFF0C;
<!-- 关于 call 和 apply 的模拟实现，可以查看[《JavaScript深入之call和apply的模拟实现》](https://github.com/mqyqingfeng/Blog/issues/11)。我们来写第一版的代码： --></p>
<pre class="language-"><code class="lang-js"><span class="token comment">// &#x7B2C;&#x4E00;&#x7248;</span>
<span class="token class-name">Function</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">bind2</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">context</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> self <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token function">self</span><span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span>context<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

<span class="token punctuation">}</span>
</code></pre>
<p>&#x6B64;&#x5916;&#xFF0C;&#x4E4B;&#x6240;&#x4EE5; <code>return self.apply(context)</code>&#xFF0C;&#x662F;&#x8003;&#x8651;&#x5230;&#x7ED1;&#x5B9A;&#x51FD;&#x6570;&#x53EF;&#x80FD;&#x662F;&#x6709;&#x8FD4;&#x56DE;&#x503C;&#x7684;&#xFF0C;&#x4F9D;&#x7136;&#x662F;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre class="language-"><code class="lang-js"><span class="token keyword">var</span> foo <span class="token operator">=</span> <span class="token punctuation">{</span>
    value<span class="token punctuation">:</span> <span class="token number">1</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">function</span> <span class="token function">bar</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>value<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> bindFoo <span class="token operator">=</span> <span class="token function">bar</span><span class="token punctuation">.</span><span class="token function">bind</span><span class="token punctuation">(</span>foo<span class="token punctuation">)</span><span class="token punctuation">;</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">bindFoo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 1</span>
</code></pre>
<h2 id="&#x4F20;&#x53C2;&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;"><a name="&#x4F20;&#x53C2;&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;" class="anchor-navigation-ex-anchor" href="#&#x4F20;&#x53C2;&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4F20;&#x53C2;&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;</h2>
<p>&#x63A5;&#x4E0B;&#x6765;&#x770B;&#x7B2C;&#x4E8C;&#x70B9;&#xFF0C;&#x53EF;&#x4EE5;&#x4F20;&#x5165;&#x53C2;&#x6570;&#x3002;&#x8FD9;&#x4E2A;&#x5C31;&#x6709;&#x70B9;&#x8BA9;&#x4EBA;&#x8D39;&#x89E3;&#x4E86;&#xFF0C;&#x6211;&#x5728; bind &#x7684;&#x65F6;&#x5019;&#xFF0C;&#x662F;&#x5426;&#x53EF;&#x4EE5;&#x4F20;&#x53C2;&#x5462;&#xFF1F;&#x6211;&#x5728;&#x6267;&#x884C; bind &#x8FD4;&#x56DE;&#x7684;&#x51FD;&#x6570;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x53EF;&#x4E0D;&#x53EF;&#x4EE5;&#x4F20;&#x53C2;&#x5462;&#xFF1F;&#x8BA9;&#x6211;&#x4EEC;&#x770B;&#x4E2A;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre class="language-"><code class="lang-js"><span class="token keyword">var</span> foo <span class="token operator">=</span> <span class="token punctuation">{</span>
    value<span class="token punctuation">:</span> <span class="token number">1</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">function</span> <span class="token function">bar</span><span class="token punctuation">(</span><span class="token parameter">name<span class="token punctuation">,</span> age</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token punctuation">}</span>

<span class="token keyword">var</span> bindFoo <span class="token operator">=</span> <span class="token function">bar</span><span class="token punctuation">.</span><span class="token function">bind</span><span class="token punctuation">(</span>foo<span class="token punctuation">,</span> <span class="token string">&apos;daisy&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">bindFoo</span><span class="token punctuation">(</span><span class="token string">&apos;18&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 1</span>
<span class="token comment">// daisy</span>
<span class="token comment">// 18</span>
</code></pre>
<p>&#x51FD;&#x6570;&#x9700;&#x8981;&#x4F20; name &#x548C; age &#x4E24;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x7ADF;&#x7136;&#x8FD8;&#x53EF;&#x4EE5;&#x5728; bind &#x7684;&#x65F6;&#x5019;&#xFF0C;&#x53EA;&#x4F20;&#x4E00;&#x4E2A; name&#xFF0C;&#x5728;&#x6267;&#x884C;&#x8FD4;&#x56DE;&#x7684;&#x51FD;&#x6570;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x518D;&#x4F20;&#x53E6;&#x4E00;&#x4E2A;&#x53C2;&#x6570; age!</p>
<p>&#x8FD9;&#x53EF;&#x548B;&#x529E;&#xFF1F;&#x4E0D;&#x6025;&#xFF0C;&#x6211;&#x4EEC;&#x7528; arguments &#x8FDB;&#x884C;&#x5904;&#x7406;&#xFF1A;</p>
<pre class="language-"><code class="lang-js"><span class="token comment">// &#x7B2C;&#x4E8C;&#x7248;</span>
<span class="token class-name">Function</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">bind2</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">context</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">var</span> self <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">;</span>
    <span class="token comment">// &#x83B7;&#x53D6;bind2&#x51FD;&#x6570;&#x4ECE;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x5230;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x53C2;&#x6570;</span>
    <span class="token keyword">var</span> args <span class="token operator">=</span> <span class="token class-name">Array</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>arguments<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// &#x8FD9;&#x4E2A;&#x65F6;&#x5019;&#x7684;arguments&#x662F;&#x6307;bind&#x8FD4;&#x56DE;&#x7684;&#x51FD;&#x6570;&#x4F20;&#x5165;&#x7684;&#x53C2;&#x6570;</span>
        <span class="token keyword">var</span> bindArgs <span class="token operator">=</span> <span class="token class-name">Array</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>arguments<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token function">self</span><span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span>context<span class="token punctuation">,</span> args<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>bindArgs<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

<span class="token punctuation">}</span>
</code></pre>
<h2 id="&#x6784;&#x9020;&#x51FD;&#x6570;&#x6548;&#x679C;&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;"><a name="&#x6784;&#x9020;&#x51FD;&#x6570;&#x6548;&#x679C;&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;" class="anchor-navigation-ex-anchor" href="#&#x6784;&#x9020;&#x51FD;&#x6570;&#x6548;&#x679C;&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6784;&#x9020;&#x51FD;&#x6570;&#x6548;&#x679C;&#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;</h2>
<p>&#x5B8C;&#x6210;&#x4E86;&#x8FD9;&#x4E24;&#x70B9;&#xFF0C;&#x6700;&#x96BE;&#x7684;&#x90E8;&#x5206;&#x5230;&#x5566;&#xFF01;&#x56E0;&#x4E3A; bind &#x8FD8;&#x6709;&#x4E00;&#x4E2A;&#x7279;&#x70B9;&#xFF0C;&#x5C31;&#x662F;</p>
<blockquote>
<p>&#x4E00;&#x4E2A;&#x7ED1;&#x5B9A;&#x51FD;&#x6570;&#x4E5F;&#x80FD;&#x4F7F;&#x7528;new&#x64CD;&#x4F5C;&#x7B26;&#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#xFF1A;&#x8FD9;&#x79CD;&#x884C;&#x4E3A;&#x5C31;&#x50CF;&#x628A;&#x539F;&#x51FD;&#x6570;&#x5F53;&#x6210;&#x6784;&#x9020;&#x5668;&#x3002;&#x63D0;&#x4F9B;&#x7684; this &#x503C;&#x88AB;&#x5FFD;&#x7565;&#xFF0C;&#x540C;&#x65F6;&#x8C03;&#x7528;&#x65F6;&#x7684;&#x53C2;&#x6570;&#x88AB;&#x63D0;&#x4F9B;&#x7ED9;&#x6A21;&#x62DF;&#x51FD;&#x6570;&#x3002;</p>
</blockquote>
<p>&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x5F53; bind &#x8FD4;&#x56DE;&#x7684;&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x65F6;&#x5019;&#xFF0C;bind &#x65F6;&#x6307;&#x5B9A;&#x7684; this &#x503C;&#x4F1A;&#x5931;&#x6548;&#xFF0C;&#x4F46;&#x4F20;&#x5165;&#x7684;&#x53C2;&#x6570;&#x4F9D;&#x7136;&#x751F;&#x6548;&#x3002;&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre class="language-"><code class="lang-js"><span class="token keyword">var</span> value <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>

<span class="token keyword">var</span> foo <span class="token operator">=</span> <span class="token punctuation">{</span>
    value<span class="token punctuation">:</span> <span class="token number">1</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">function</span> <span class="token function">bar</span><span class="token punctuation">(</span><span class="token parameter">name<span class="token punctuation">,</span> age</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>habit <span class="token operator">=</span> <span class="token string">&apos;shopping&apos;</span><span class="token punctuation">;</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

bar<span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>friend <span class="token operator">=</span> <span class="token string">&apos;kevin&apos;</span><span class="token punctuation">;</span>

<span class="token keyword">var</span> bindFoo <span class="token operator">=</span> <span class="token function">bar</span><span class="token punctuation">.</span><span class="token function">bind</span><span class="token punctuation">(</span>foo<span class="token punctuation">,</span> <span class="token string">&apos;daisy&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">bindFoo</span><span class="token punctuation">(</span><span class="token string">&apos;18&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// undefined</span>
<span class="token comment">// daisy</span>
<span class="token comment">// 18</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>obj<span class="token punctuation">.</span>habit<span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>obj<span class="token punctuation">.</span>friend<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// shopping</span>
<span class="token comment">// kevin</span>
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x5C3D;&#x7BA1;&#x5728;&#x5168;&#x5C40;&#x548C; foo &#x4E2D;&#x90FD;&#x58F0;&#x660E;&#x4E86; value &#x503C;&#xFF0C;&#x6700;&#x540E;&#x4F9D;&#x7136;&#x8FD4;&#x56DE;&#x4E86; undefind&#xFF0C;&#x8BF4;&#x660E;&#x7ED1;&#x5B9A;&#x7684; this &#x5931;&#x6548;&#x4E86;&#xFF0C;&#x5982;&#x679C;&#x5927;&#x5BB6;&#x4E86;&#x89E3; new &#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#xFF0C;&#x5C31;&#x4F1A;&#x77E5;&#x9053;&#x8FD9;&#x4E2A;&#x65F6;&#x5019;&#x7684; this &#x5DF2;&#x7ECF;&#x6307;&#x5411;&#x4E86; obj&#x3002;</p>
<!-- (哈哈，我这是为我的下一篇文章[《JavaScript深入系列之new的模拟实现》](https://github.com/mqyqingfeng/Blog/issues/13)打广告)。 -->
<p>&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4FEE;&#x6539;&#x8FD4;&#x56DE;&#x7684;&#x51FD;&#x6570;&#x7684;&#x539F;&#x578B;&#x6765;&#x5B9E;&#x73B0;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x5199;&#x4E00;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-js"><span class="token comment">// &#x7B2C;&#x4E09;&#x7248;</span>
<span class="token class-name">Function</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">bind2</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">context</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> self <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> args <span class="token operator">=</span> <span class="token class-name">Array</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>arguments<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">var</span> <span class="token function-variable function">fBound</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">var</span> bindArgs <span class="token operator">=</span> <span class="token class-name">Array</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>arguments<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// &#x5F53;&#x4F5C;&#x4E3A;&#x6784;&#x9020;&#x51FD;&#x6570;&#x65F6;&#xFF0C;this &#x6307;&#x5411;&#x5B9E;&#x4F8B;&#xFF0C;&#x6B64;&#x65F6;&#x7ED3;&#x679C;&#x4E3A; true&#xFF0C;&#x5C06;&#x7ED1;&#x5B9A;&#x51FD;&#x6570;&#x7684; this &#x6307;&#x5411;&#x8BE5;&#x5B9E;&#x4F8B;&#xFF0C;&#x53EF;&#x4EE5;&#x8BA9;&#x5B9E;&#x4F8B;&#x83B7;&#x5F97;&#x6765;&#x81EA;&#x7ED1;&#x5B9A;&#x51FD;&#x6570;&#x7684;&#x503C;</span>
        <span class="token comment">// &#x4EE5;&#x4E0A;&#x9762;&#x7684;&#x662F; demo &#x4E3A;&#x4F8B;&#xFF0C;&#x5982;&#x679C;&#x6539;&#x6210; `this instanceof fBound ? null : context`&#xFF0C;&#x5B9E;&#x4F8B;&#x53EA;&#x662F;&#x4E00;&#x4E2A;&#x7A7A;&#x5BF9;&#x8C61;&#xFF0C;&#x5C06; null &#x6539;&#x6210; this &#xFF0C;&#x5B9E;&#x4F8B;&#x4F1A;&#x5177;&#x6709; habit &#x5C5E;&#x6027;</span>
        <span class="token comment">// &#x5F53;&#x4F5C;&#x4E3A;&#x666E;&#x901A;&#x51FD;&#x6570;&#x65F6;&#xFF0C;this &#x6307;&#x5411; window&#xFF0C;&#x6B64;&#x65F6;&#x7ED3;&#x679C;&#x4E3A; false&#xFF0C;&#x5C06;&#x7ED1;&#x5B9A;&#x51FD;&#x6570;&#x7684; this &#x6307;&#x5411; context</span>
        <span class="token keyword">return</span> <span class="token function">self</span><span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span><span class="token keyword">this</span> <span class="token keyword">instanceof</span> <span class="token class-name">fBound</span> <span class="token operator">?</span> <span class="token keyword">this</span> <span class="token punctuation">:</span> context<span class="token punctuation">,</span> args<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>bindArgs<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// &#x4FEE;&#x6539;&#x8FD4;&#x56DE;&#x51FD;&#x6570;&#x7684; prototype &#x4E3A;&#x7ED1;&#x5B9A;&#x51FD;&#x6570;&#x7684; prototype&#xFF0C;&#x5B9E;&#x4F8B;&#x5C31;&#x53EF;&#x4EE5;&#x7EE7;&#x627F;&#x7ED1;&#x5B9A;&#x51FD;&#x6570;&#x7684;&#x539F;&#x578B;&#x4E2D;&#x7684;&#x503C;</span>
    fBound<span class="token punctuation">.</span>prototype <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>prototype<span class="token punctuation">;</span>
    <span class="token keyword">return</span> fBound<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<!-- 如果对原型链稍有困惑，可以查看[《JavaScript深入之从原型到原型链》](https://github.com/mqyqingfeng/Blog/issues/2)。 -->
<h2 id="&#x6784;&#x9020;&#x51FD;&#x6570;&#x6548;&#x679C;&#x7684;&#x4F18;&#x5316;&#x5B9E;&#x73B0;"><a name="&#x6784;&#x9020;&#x51FD;&#x6570;&#x6548;&#x679C;&#x7684;&#x4F18;&#x5316;&#x5B9E;&#x73B0;" class="anchor-navigation-ex-anchor" href="#&#x6784;&#x9020;&#x51FD;&#x6570;&#x6548;&#x679C;&#x7684;&#x4F18;&#x5316;&#x5B9E;&#x73B0;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6784;&#x9020;&#x51FD;&#x6570;&#x6548;&#x679C;&#x7684;&#x4F18;&#x5316;&#x5B9E;&#x73B0;</h2>
<p>&#x4F46;&#x662F;&#x5728;&#x8FD9;&#x4E2A;&#x5199;&#x6CD5;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x76F4;&#x63A5;&#x5C06; fBound.prototype = this.prototype&#xFF0C;&#x6211;&#x4EEC;&#x76F4;&#x63A5;&#x4FEE;&#x6539; fBound.prototype &#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4E5F;&#x4F1A;&#x76F4;&#x63A5;&#x4FEE;&#x6539;&#x7ED1;&#x5B9A;&#x51FD;&#x6570;&#x7684; prototype&#x3002;&#x8FD9;&#x4E2A;&#x65F6;&#x5019;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4E00;&#x4E2A;&#x7A7A;&#x51FD;&#x6570;&#x6765;&#x8FDB;&#x884C;&#x4E2D;&#x8F6C;&#xFF1A;</p>
<pre class="language-"><code class="lang-js"><span class="token comment">// &#x7B2C;&#x56DB;&#x7248;</span>
<span class="token class-name">Function</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">bind2</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">context</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">var</span> self <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> args <span class="token operator">=</span> <span class="token class-name">Array</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>arguments<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">var</span> <span class="token function-variable function">fNOP</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>

    <span class="token keyword">var</span> <span class="token function-variable function">fBound</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">var</span> bindArgs <span class="token operator">=</span> <span class="token class-name">Array</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>arguments<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token function">self</span><span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span><span class="token keyword">this</span> <span class="token keyword">instanceof</span> <span class="token class-name">fNOP</span> <span class="token operator">?</span> <span class="token keyword">this</span> <span class="token punctuation">:</span> context<span class="token punctuation">,</span> args<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>bindArgs<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    fNOP<span class="token punctuation">.</span>prototype <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>prototype<span class="token punctuation">;</span>
    fBound<span class="token punctuation">.</span>prototype <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">fNOP</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> fBound<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5230;&#x6B64;&#x4E3A;&#x6B62;&#xFF0C;&#x5927;&#x7684;&#x95EE;&#x9898;&#x90FD;&#x5DF2;&#x7ECF;&#x89E3;&#x51B3;&#xFF0C;&#x7ED9;&#x81EA;&#x5DF1;&#x4E00;&#x4E2A;&#x8D5E;&#xFF01;o(&#xFFE3;&#x25BD;&#xFFE3;)&#xFF44;</p>
<h2 id="&#x4E09;&#x4E2A;&#x5C0F;&#x95EE;&#x9898;"><a name="&#x4E09;&#x4E2A;&#x5C0F;&#x95EE;&#x9898;" class="anchor-navigation-ex-anchor" href="#&#x4E09;&#x4E2A;&#x5C0F;&#x95EE;&#x9898;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E09;&#x4E2A;&#x5C0F;&#x95EE;&#x9898;</h2>
<p>&#x63A5;&#x4E0B;&#x6765;&#x5904;&#x7406;&#x4E9B;&#x5C0F;&#x95EE;&#x9898;:</p>
<p><strong>1.apply &#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x8DDF; MDN &#x4E0A;&#x7684;&#x7A0D;&#x6709;&#x4E0D;&#x540C;</strong></p>
<p>&#x5728; MDN &#x4E2D;&#x6587;&#x7248;&#x8BB2; bind &#x7684;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#x65F6;&#xFF0C;apply &#x8FD9;&#x91CC;&#x7684;&#x4EE3;&#x7801;&#x662F;&#xFF1A;</p>
<pre class="language-"><code class="lang-js">
<span class="token function">self</span><span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span><span class="token keyword">this</span> <span class="token keyword">instanceof</span> <span class="token class-name">self</span> <span class="token operator">?</span> <span class="token keyword">this</span> <span class="token punctuation">:</span> context <span class="token operator">||</span> <span class="token keyword">this</span><span class="token punctuation">,</span> args<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>bindArgs<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre>
<p>&#x591A;&#x4E86;&#x4E00;&#x4E2A;&#x5173;&#x4E8E; context &#x662F;&#x5426;&#x5B58;&#x5728;&#x7684;&#x5224;&#x65AD;&#xFF0C;&#x7136;&#x800C;&#x8FD9;&#x4E2A;&#x662F;&#x9519;&#x8BEF;&#x7684;&#xFF01;</p>
<p>&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre class="language-"><code class="lang-js"><span class="token keyword">var</span> value <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> foo <span class="token operator">=</span> <span class="token punctuation">{</span>
    value<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
    bar<span class="token punctuation">:</span> <span class="token function">bar</span><span class="token punctuation">.</span><span class="token function">bind</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">function</span> <span class="token function">bar</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

foo<span class="token punctuation">.</span><span class="token function">bar</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 2</span>
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x4EE3;&#x7801;&#x6B63;&#x5E38;&#x60C5;&#x51B5;&#x4E0B;&#x4F1A;&#x6253;&#x5370; 2&#xFF0C;&#x5982;&#x679C;&#x6362;&#x6210;&#x4E86; context || this&#xFF0C;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x5C31;&#x4F1A;&#x6253;&#x5370; 1&#xFF01;</p>
<p>&#x6240;&#x4EE5;&#x8FD9;&#x91CC;&#x4E0D;&#x5E94;&#x8BE5;&#x8FDB;&#x884C; context &#x7684;&#x5224;&#x65AD;&#xFF0C;&#x5927;&#x5BB6;&#x67E5;&#x770B; MDN &#x540C;&#x6837;&#x5185;&#x5BB9;&#x7684;&#x82F1;&#x6587;&#x7248;&#xFF0C;&#x5C31;&#x4E0D;&#x5B58;&#x5728;&#x8FD9;&#x4E2A;&#x5224;&#x65AD;&#xFF01;</p>
<p><strong>2.&#x8C03;&#x7528; bind &#x7684;&#x4E0D;&#x662F;&#x51FD;&#x6570;&#x548B;&#x529E;&#xFF1F;</strong></p>
<p>&#x4E0D;&#x884C;&#xFF0C;&#x6211;&#x4EEC;&#x8981;&#x62A5;&#x9519;&#xFF01;</p>
<pre class="language-"><code class="lang-js"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">typeof</span> <span class="token keyword">this</span> <span class="token operator">!==</span> <span class="token string">&quot;function&quot;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">&quot;Function.prototype.bind - what is trying to be bound is not callable&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p><strong>3.&#x6211;&#x8981;&#x5728;&#x7EBF;&#x4E0A;&#x7528;</strong></p>
<p>&#x90A3;&#x522B;&#x5FD8;&#x4E86;&#x505A;&#x4E2A;&#x517C;&#x5BB9;&#xFF1A;</p>
<pre class="language-"><code class="lang-js"><span class="token class-name">Function</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>bind <span class="token operator">=</span> <span class="token class-name">Function</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>bind <span class="token operator">||</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    &#x2026;&#x2026;
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre>
<p>&#x5F53;&#x7136;&#x6700;&#x597D;&#x662F;&#x7528; <a href="https://github.com/es-shims/es5-shim" target="_blank">es5-shim</a> &#x5566;&#x3002;</p>
<h2 id="&#x6700;&#x7EC8;&#x4EE3;&#x7801;"><a name="&#x6700;&#x7EC8;&#x4EE3;&#x7801;" class="anchor-navigation-ex-anchor" href="#&#x6700;&#x7EC8;&#x4EE3;&#x7801;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6700;&#x7EC8;&#x4EE3;&#x7801;</h2>
<p>&#x6240;&#x4EE5;&#x6700;&#x6700;&#x540E;&#x7684;&#x4EE3;&#x7801;&#x5C31;&#x662F;&#xFF1A;</p>
<pre class="language-"><code class="lang-js"><span class="token class-name">Function</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">bind2</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">context</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">typeof</span> <span class="token keyword">this</span> <span class="token operator">!==</span> <span class="token string">&quot;function&quot;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">&quot;Function.prototype.bind - what is trying to be bound is not callable&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">var</span> self <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> args <span class="token operator">=</span> <span class="token class-name">Array</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>arguments<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">var</span> <span class="token function-variable function">fNOP</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>

    <span class="token keyword">var</span> <span class="token function-variable function">fBound</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">var</span> bindArgs <span class="token operator">=</span> <span class="token class-name">Array</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>arguments<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token function">self</span><span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span><span class="token keyword">this</span> <span class="token keyword">instanceof</span> <span class="token class-name">fNOP</span> <span class="token operator">?</span> <span class="token keyword">this</span> <span class="token punctuation">:</span> context<span class="token punctuation">,</span> args<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>bindArgs<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    fNOP<span class="token punctuation">.</span>prototype <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>prototype<span class="token punctuation">;</span>
    fBound<span class="token punctuation">.</span>prototype <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">fNOP</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> fBound<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; YoungGary 2019 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2020-01-03 16:31:06
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="el.html" class="navigation navigation-prev " aria-label="Previous page: 2.Element UI 源码初探">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="proto.html" class="navigation navigation-next " aria-label="Next page: 4.JS原型和原型链">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"3.bind的模拟实现","level":"1.5.3","depth":2,"next":{"title":"4.JS原型和原型链","level":"1.5.4","depth":2,"path":"js/proto.md","ref":"js/proto.md","articles":[]},"previous":{"title":"2.Element UI 源码初探","level":"1.5.2","depth":2,"path":"js/el.md","ref":"js/el.md","articles":[]},"dir":"ltr"},"config":{"plugins":["theme-comscore","prism","-highlight","copy-code-button","search-pro","-search","-lunr","expandable-chapters","splitter","-sharing","tbfed-pagefooter","anchor-navigation-ex"],"styles":{},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy YoungGary 2019","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"prism":{"css":["prismjs/themes/prism-solarizedlight.css"],"lang":{"shell":"bash"}},"splitter":{},"search-pro":{},"fontsettings":{"theme":"white","family":"sans","size":2},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":false},"theme-comscore":{},"copy-code-button":{},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","author":"Gary","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Gary的小窝","language":"zh-hans","gitbook":"*","description":"Gary的小窝. 里面包含了个人撰写的技术文章"},"file":{"path":"js/bind.md","mtime":"2020-01-03T08:31:06.073Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-09-09T14:10:10.930Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-copy-code-button/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

